<?php
if (!defined('IN_MEDIA')) die("Url not invail");
class raovat{
	public $id=0, $title="", $cost="", $mota="", $soluong="", 
		$ngaytao="", $songay="", $categories="", $img="", $huyen="", $status=0, $user="";
	function tin($id){
		global $tb_prefix, $mysql;		
		$query=mysql_query("SELECT * FROM ".$tb_prefix."rao_vat WHERE SALE_ID='".$id."'" );
		while($row = mysql_fetch_array($query)){	
			$this->id=$row['SALE_ID'];
			$this->tilte=$row['SALE_TITLE'];	
			$this->cost=$row['SALE_COST'];
			$this->mota=$row['SALE_DESCRIPTION'];
			$this->soluong=$row['SALE_SLG'];
			$this->ngaytao=$row['SALE_TIME'];
			$this->songay=$row['SALE_TIME_EX'];
			$this->categories=$row['SALE_IDCATE'];
			$this->img=$row['SP_IMG'];
			$this->huyen=$row['SP_HUYEN'];
			$this->status=$row['SP_STATUS'];
			$this->user=$row['SP_USER_ID'];
		}
	}
}
//Lấy TIN THUỘC LEVEL 3
function level3($id,$start=-1, $end=-1){
	$id=mysql_real_escape_string($id);
	global $tb_prefix;
	if (get_data('SP_TYPE','categories','SP_ID',$id)=="level1"){
		$q=mysql_query("SELECT SP_ID FROM ".$tb_prefix."categories WHERE SP_TYPE='level2' and SP_ID_FA='".$id."'");
		$query="";
		$num=mysql_num_rows($q);
		$i=0;
		$idle3=array();
		while($row = mysql_fetch_array($q)){
			$q1=mysql_query("SELECT SP_ID FROM ".$tb_prefix."categories WHERE SP_TYPE='level3' and SP_ID_FA='".$row['SP_ID']."'");
			while($row1=mysql_fetch_array($q1)){
				if($i==0)$query .="(SELECT * FROM ".$tb_prefix."rao_vat WHERE SALE_IDCATE='".$row1['SP_ID']."') ";
				else $query .="UNION (SELECT * FROM ".$tb_prefix."rao_vat WHERE SALE_IDCATE='".$row1['SP_ID']."') ";
				$i++;
			}
		}
		if ($query!=""){
			if ($end==-1) $query=$query." "; 
			else $query=$query." ORDER BY SALE_TIME DESC LIMIT ".$start.", ".$end; 
			$q2=mysql_query($query);
			while($row3=mysql_fetch_array($q2))$idle3[]=$row3['SALE_ID'];	
		}
		return $idle3;
	}
	if (get_data('SP_TYPE','categories','SP_ID',$id)=="level2"){
		$q=mysql_query("SELECT SP_ID FROM ".$tb_prefix."categories WHERE SP_TYPE='level3' and SP_ID_FA='".$id."'");
		$query="";
		$num=mysql_num_rows($q);
		$i=0;
		$idle3=array();
		while($row = mysql_fetch_array($q)){
				if($i==0)$query .="(SELECT * FROM ".$tb_prefix."rao_vat WHERE SALE_IDCATE='".$row['SP_ID']."') ";
				else $query .="UNION (SELECT * FROM ".$tb_prefix."rao_vat WHERE SALE_IDCATE='".$row['SP_ID']."') ";
				$i++;
		}
		if ($query!=""){
			if ($end==-1) $query=$query.""; 
			else $query=$query." ORDER BY SALE_TIME DESC LIMIT ".$start.", ".$end; 
			$q2=mysql_query($query);
			while($row3=mysql_fetch_array($q2))$idle3[]=$row3['SALE_ID'];	
		}
		return $idle3;
	}
	if (get_data('SP_TYPE','categories','SP_ID',$id)=="level3"){
		$id=mysql_real_escape_string($id);
		global $tb_prefix;
		if ($end==-1) $q=mysql_query("SELECT SALE_ID FROM ".$tb_prefix."rao_vat WHERE SALE_IDCATE='".$id."' ORDER BY SALE_TIME DESC");
		else $q=mysql_query("SELECT SALE_ID FROM ".$tb_prefix."rao_vat WHERE SALE_IDCATE='".$id."' ORDER BY SALE_TIME DESC LIMIT ".$start.", ".$end);
		$num=mysql_num_rows($q);
		$idle3=array();
		while($row = mysql_fetch_array($q)){
			$idle3[]=$row['SALE_ID'];
		}
		return $idle3;
		
	}
}
// Xác định level của MENU
function islevel($id){
	$id=mysql_real_escape_string($id);
	global $tb_prefix;
	$q=mysql_query("SELECT SP_TYPE FROM ".$tb_prefix."categories WHERE SP_ID='".$id."' ");	
	while($row=mysql_fetch_array($q)){
		 if ($row['SP_TYPE']=='level1') return 1;
		 if ($row['SP_TYPE']=='level2') return 2;
	     if ($row['SP_TYPE']=='level3') return 3;
	}
}


?>